/**
 * Created by bbb
 */
import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    AsyncStorage,
    View,
  AppState,
    NetInfo
} from 'react-native';
import { MtContainer, MtSeparator, MtChar, MtColor, MtActionButton, MtToast } from 'martian-ui';
import {Navigator} from 'react-native-deprecated-custom-components';
import { connect } from 'react-redux';
import TabViewController from '../container/TabViewController';
import YaguanHome from '../container/YaguanHome';
import Login from '../container/Login';
import Sensor from './Devices/Sensor'
import { persistStore } from '../action/store';

var netAvailable = true;
function setNetAvailable(value) {
  netAvailable = value;
}

let firstUnknown = true;
function setNetInfo(origin) {
  const netState = transformNetState(origin);
  if (netState !== 'unconnected' && netState !== 'unknown') {
    setNetAvailable(true);
    // console.log('netState:', netState);
  }
  else {
    MtToast.show('请检查网络');
    // console.log('netState:', netState);
  }
  // console.log('netState:', netState);
  return {
    type: 'SET_NET_INFO',
    payload: { netState },
  };
}


function transformNetState(value) {
  switch (value) {
    case 'none':
    case 'NONE':
      //小优化、无连接时组织中间件发送网络请求
      return 'unconnected';
    case 'wifi':
    case 'WIFI':
      return 'wifi';
    case 'cell':
    case 'WIMAX':
    case 'MOBILE_DUN':
    case 'MOBILE_HIPRI':
    case 'MOBILE_MMS':
    case 'MOBILE_SUPL':
    case 'MOBILE':
      return 'cell';
    default:
      return value.toLowerCase();
  }
}


function readAssessToken() {
    AsyncStorage.getItem('assessToken',(error,result)=>{
        if (!error) {
            console.log(result);
        }
        result && this.setState({assessToken: JSON.stringify(result)} );
        this.setState({show: true} );

    })
}
class Home extends React.Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            assessToken: '',
            show: false,
        };
        this._readAssessToken = readAssessToken.bind(this);
      this._handleAppStateChange = this.handleAppStateChange.bind(this);
      this._handleConnectivityChangeHandle = this.handleConnectivityChangeHandle.bind(this);
    }
    componentWillMount() {
        // SplashScreen.hide();//关闭启动屏幕
        // RNBridgeModule.login("bianbian","123456").then((events)=>{
        //     console.warn(JSON.stringify(events));
        // }).catch((e)=>{
        //     console.warn(e);
        // })
        this._readAssessToken();

    }
  componentDidMount() {
    const { dispatch, store } = this.props;
    AppState.addEventListener('change', this._handleAppStateChange);
    NetInfo.fetch().done((connectionInfo) => {
      dispatch(setNetInfo(connectionInfo));

    });
    NetInfo.addEventListener('change', this._handleConnectivityChangeHandle);


  }
  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
    NetInfo.removeEventListener('change', this._handleConnectivityChangeHandle);


  }

  handleAppStateChange(appState) {
    const { dispatch } = this.props;

    switch (appState) {
      case 'inactive':
      case 'background':
      {
         dispatch(persistStore());
        break;
      }
      case 'active':
      {


        break;
      }
      default:
        break;
    }
  }
  handleConnectivityChangeHandle(connectionInfo) {
    this.props.dispatch(setNetInfo(connectionInfo));
  }

  render() {
        const {assessToken, show} = this.state;
        let navigatorCompont;

        if(JSON.stringify(assessToken )=== 'null' || !assessToken){
            navigatorCompont ={name: 'Login', component: Login}
        }
        else {
            navigatorCompont=  {name: 'TabViewController', component: TabViewController}
        }

        return (
                show &&
                <Navigator
                    initialRoute={navigatorCompont}
                    renderScene={(route, navigator) => {
                        let Component = route.component;
                        return <Component {...route.params} navigator={navigator}/>
                    }}
                />


        )
    }
}
module.exports = connect(select)(Home);

function select(store) {
  return {

  };
}
